package com.example.booklist.controller;

import com.example.booklist.constant.UserConstant;
import com.example.booklist.model.*;
import com.example.booklist.service.BookService;
import com.example.booklist.service.UserService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;

@RequestMapping("/book")
@RestController
@Slf4j
public class BookController {

    @Autowired
    private BookService bookService;

    @RequestMapping("/getListByPage")
    public Result getListByPage(PageRequest pageRequest, HttpSession session) {
        log.info("接收到请求，开始获取每一页的图书数");
        if(pageRequest.getCurrentPage() < 1) {
            return null;
        }
        PageResult<BookInfo> booksInfo = null;
        try {
            booksInfo = bookService.getBookByPage(pageRequest);
            return Result.success(booksInfo);
        }catch (Exception e) {
            log.error("查询翻页错误:{}",e);
            return Result.fail();
        }
    }

    //添加图书
    @RequestMapping(value = "/addBook",produces = "application/json")
    public String addBook(BookInfo bookInfo) {
        log.info("接受到请求，开始添加图书");
        //参数校验
        if(!StringUtils.hasLength(bookInfo.getBookName())
            || !StringUtils.hasLength(bookInfo.getAuthor())
            || bookInfo.getCount() < 0
            || !StringUtils.hasLength(bookInfo.getPublish())
            || bookInfo.getPrice().compareTo(new BigDecimal(0)) < 0) {
            log.error("参数校验出错");
            return "添加图书失败，请联系管理员";
        }
        try {
            bookService.setBook(bookInfo);
        }catch (Exception e) {
            log.error("添加图书失败，e:{}",e);
            return "添加图书失败，请联系管理员";
        }
        //添加成功直接返回空字符串
        return "";
    }

    //删除图书
    @RequestMapping(value = "/deleteBook",produces = "application/json")
    public String deleteBook(BookInfo bookInfo) {
        log.info("接受到请求，开始删除图书，根据ID");
        if(bookInfo.getId() < 0) {
            log.error("参数校验失败");
            return "删除图书失败，请联系管理员";
        }
        try {
            bookService.deleteById(bookInfo);
        }catch (Exception e) {
            log.error("Controller出现异常:{}",e);
            return "删除图书失败，请联系管理员";
        }
        return "";
    }

    //根据Id查询图书
    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(@RequestParam("bookId") Integer id) {
        log.info("接受到请求，开始查询图书，根据ID");
        if(id < 0) {
            log.error("参数校验失败");
            return null;
        }
        BookInfo book = null;
        try {
            book = bookService.getBookById(id);
        }catch (Exception e) {
            log.error("Controller出现异常:{}",e);
            return null;
        }
        return book;
    }


    //更新图书
    @RequestMapping(value = "/updateBook",produces = "application/json")
    public String updateBook(BookInfo bookInfo) {
        log.info("接受到请求，开始更新图书");
        //参数校验
        if(!StringUtils.hasLength(bookInfo.getBookName())
                || !StringUtils.hasLength(bookInfo.getAuthor())
                || bookInfo.getCount() < 0
                || !StringUtils.hasLength(bookInfo.getPublish())
                || bookInfo.getPrice().compareTo(new BigDecimal(0)) < 0
                || bookInfo.getId() < 0) {
            log.error("参数校验出错");
            return "更新图书失败，请联系管理员";
        }
        try {
            bookService.updateBookById(bookInfo);
        }catch (Exception e) {
            log.error("Controller出现异常:{}",e);
            return "更新图书失败，请联系管理员";
        }
        return "";
    }

    @RequestMapping(value = "/batchDelete",produces = "application/json")
    public String batchDelete(@RequestParam List<Integer> ids) {
        log.info("接收到请求，开始批量删除");
        if(ids == null || ids.size() < 1) {
            return "";
        }
        try {
            bookService.deleteBookBatch(ids);
        }catch (Exception e) {
            log.error("批量删除失败:{}",e);
            return "批量删除失败";
        }
        return "";
    }
}
